package de.fstyle.android.worldcanvas;

import de.fstyle.android.worldcanvas.fragments.ColorFragment;
import de.fstyle.android.worldcanvas.model.ObjectDescription;
import de.fstyle.android.worldcanvas.util.HttpUtils;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

public class SendColorAsyncTask 
extends AsyncTask<ObjectDescription<?>, Object, Object> {

	public static final int INDEX_OBJECT = 0;
	public static final int INDEX_META = 1;
	public static final int INDEX_LOCATION = 2;

	private ColorFragment fragment;
	
	public SendColorAsyncTask(ColorFragment fragment) {
		this.fragment = fragment;
	}
	
	@Override
	protected Object doInBackground(ObjectDescription<?>... args) {
		
		Handler handler = new Handler(Looper.getMainLooper());
		
		// check network connection
		if (!this.fragment.isOnline()) {
			handler.post(new Runnable() {
				@Override
				public void run() {
					Toast toast = Toast.makeText(
							SendColorAsyncTask.this.fragment.getActivity(), 
							"No internet connection.\n" +
									"New color was not sent.\n" +
									"Try again later.", 
							Toast.LENGTH_LONG);
					toast.show();
				}
			});
		} else {
			for (ObjectDescription<?> o : args) {
				HttpUtils.send(o, SendColorAsyncTask.this.fragment.getActivity());
			}
		}
		
		return null;
	}
}
